/**
 * Created by zoushuiyun on 2017/7/25.
 */
import { Injectable }       from '@angular/core';
import {
  CanActivate, Router,
  ActivatedRouteSnapshot,
  RouterStateSnapshot
}                           from '@angular/router';
import {CacheService} from "../../shared/service/cache.service";
import {myValidNull} from "../../shared/util/string-utils";

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private cacheService: CacheService, private router: Router) {}

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    let url: string = state.url;

    return this.checkLogin(url);
  }

  /**
   * 验证是否登录 有用户相关缓存信息，否则跳转至登录页
   *
   * @param url
   * @returns {boolean}
   */
  checkLogin(url: string): boolean {

    //销售点和展馆才可登录 C 端 下单端
    let userInfo:any = this.cacheService.getCurrUserInfo();
    if (!myValidNull(userInfo)&&!myValidNull(userInfo.groupId)&&(userInfo.groupId == 3 || userInfo.groupId == 4)) { return true; }

    // Store the attempted URL for redirecting
    // this.cacheService.redirectUrl = url;

    // Navigate to the login page with extras
    console.info("未登录，或缓存过期，重新登录====》login");
    this.router.navigate(['/login']);
    return false;
  }

}
